﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Gmantis.Controls.DataGrid
{
    public class DataGridGroupingPanel : Panel
    {
        // Fields
        public static readonly DependencyProperty ColumnsHeaderGapProperty = DependencyProperty.Register("ColumnsHeaderGap", typeof(double), typeof(DataGridGroupingPanel), new PropertyMetadata(4.0, new PropertyChangedCallback(DataGridGroupingPanel.OnColumnsHeaderGapPropertyChanged)));
        public static readonly DependencyProperty IndentHeightProperty = DependencyProperty.Register("IndentHeight", typeof(double), typeof(DataGridGroupingPanel), new PropertyMetadata(8.0, new PropertyChangedCallback(DataGridGroupingPanel.OnIndentHeightPropertyChanged)));

        // Methods
        protected override Size ArrangeOverride(Size finalSize)
        {
            double x = 0.0;
            double y = 0.0;
            int num3 = 0;
            foreach (DataGridGroupColumnHeaderPresenter presenter in base.Children)
            {
                presenter.Arrange(new Rect(x, y, presenter.DesiredSize.Width, Math.Max(presenter.DesiredSize.Height, this.DataGrid.ColumnHeaderHeight)));
                x += presenter.DesiredSize.Width + this.ColumnsHeaderGap;
                y = ++num3 * this.IndentHeight;
            }
            return finalSize;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            double width = 0.0;
            double num2 = 0.0;
            int num3 = 0;
            foreach (DataGridGroupColumnHeaderPresenter presenter in base.Children)
            {
                presenter.Measure(new Size(double.PositiveInfinity, this.DataGrid.ColumnHeaderHeight));
                num2 = Math.Max(num2, Math.Max(presenter.DesiredSize.Height, this.DataGrid.ColumnHeaderHeight) + (num3 * this.IndentHeight));
                width += presenter.DesiredSize.Width + this.ColumnsHeaderGap;
                num3++;
            }
            return new Size(width, num2);
        }

        private static void OnColumnsHeaderGapPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGridGroupingPanel panel = d as DataGridGroupingPanel;
            double oldValue = (double)e.OldValue;
            double newValue = (double)e.NewValue;
            if (newValue != oldValue)
            {
                panel.InvalidateMeasure();
            }
        }

        private static void OnIndentHeightPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGridGroupingPanel panel = d as DataGridGroupingPanel;
            double oldValue = (double)e.OldValue;
            double newValue = (double)e.NewValue;
            if (newValue != oldValue)
            {
                panel.InvalidateMeasure();
            }
        }

        // Properties
        public double ColumnsHeaderGap
        {
            get
            {
                return (double)base.GetValue(ColumnsHeaderGapProperty);
            }
            set
            {
                base.SetValue(ColumnsHeaderGapProperty, value);
            }
        }

        public DataGrid DataGrid
        {
            get
            {
                return this.View.DataGrid;
            }
        }

        public double IndentHeight
        {
            get
            {
                return (double)base.GetValue(IndentHeightProperty);
            }
            set
            {
                base.SetValue(IndentHeightProperty, value);
            }
        }

        internal DataGridView View { get; set; }
    }
}
